package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author pengyehua
 * @date 2020/12/5 22:51
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService{

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 根据id，name模糊查询制造商信息
     * @param brandBO
     * @return
     */
    @Override
    public ListData<Brand> queryBrand(BrandBO brandBO) {
        //分页
        PageHelper.startPage(brandBO.getPage(),brandBO.getLimit());
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if(brandBO.getId() != null){
            criteria.andIdEqualTo(brandBO.getId());
        }
        if(brandBO.getName() != null){
            criteria.andNameLike("%" + brandBO.getName() + "%");
        }
        //被删除的不能查询
        criteria.andDeletedEqualTo(false);
        //排序
        brandExample.setOrderByClause(brandBO.getSort() + " " + brandBO.getOrder());
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        ListData<Brand> brandListData = new ListData<>();
        brandListData.setItems(brands);

        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        brandListData.setTotal((int)total);
        return brandListData;
    }

    /**
     * 插入制造商信息
     * @param brand
     * @return
     */
    @Override
    public Brand insertBrand(Brand brand) {
        BrandExample brandExample = new BrandExample();
        byte sort_order =(byte) (brandMapper.countByExample(brandExample) + 1);
        Date date = new Date();
        brand.setAddTime(date);
        brand.setUpdateTime(date);
        brand.setDeleted(false);
        brand.setSortOrder(sort_order);
        brandMapper.insertSelective(brand);
        return brand;
    }

    /**
     * 删除品牌商信息
     * @param brand
     */
    @Override
    public void deleteBrand(Brand brand) {
        brand.setDeleted(true);
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 修改品牌商信息
     * @param brand
     * @return
     */
    @Override
    public Brand updateBrand(Brand brand) {
        Date date = new Date();
        brand.setUpdateTime(date);
        brandMapper.updateByPrimaryKeySelective(brand);
        return brand;
    }

    /**
     * 查询指定品牌商的商品
     * @param id
     * @return
     */
    @Override
    public int queryGoodsCount(Integer id) {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andBrandIdEqualTo(id);
        int count =(int) goodsMapper.countByExample(goodsExample);
        return count;
    }
}
